package com.zmj.sy.mom.srv.aps.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.dto.workReport.ReportDetailDto;
import com.zmj.sy.mom.srv.aps.bean.entity.workReport.ReportDetail;
import com.zmj.sy.mom.srv.aps.bean.vo.base.Result;
import com.zmj.sy.mom.srv.aps.bean.vo.workReport.ReportDetailVo;
import com.zmj.sy.mom.srv.aps.mapper.WorkReportMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@RequiredArgsConstructor
@Service
public class WorkReportService {
    private final WorkReportMapper workReportMapper;

    /**
     * @Description: 报工记录明细数据
     *
     * @author: hejiahui
     * @since: 2024/10/22 16:22
     */
    public Result<IPage<ReportDetailVo>> getReportDetail(ReportDetailDto reportDetailDto) {
        //默认今天
        if (StringUtils.isBlank(reportDetailDto.getStartTime())) {
            reportDetailDto.setStartTime(DateUtil.date().toDateStr());
        }
        if (StringUtils.isBlank(reportDetailDto.getEndTime())) {
            reportDetailDto.setEndTime(DateUtil.date().offset(DateField.DAY_OF_MONTH, 1).toDateStr());
        }

        //分页获取报工详情记录
//        Page page = new Page(reportDetailDto.getPageNum(), reportDetailDto.getPageSize());
        QueryWrapper query = new QueryWrapper();
        query.between("IFNULL(record.work_time,record.create_date_time)", reportDetailDto.getStartTime(), reportDetailDto.getEndTime());
        query.like(StringUtils.isNotBlank(reportDetailDto.getConstructNo()),"record.construct_no",reportDetailDto.getConstructNo());
        /*if (StringUtils.isNotBlank(reportDetailDto.getPackageNo()) && reportDetailDto.getPackageNo().startsWith("WP-")) {
            Integer packageNo = PkgUtils.rollBack(reportDetailDto.getPackageNo());
            query.eq("aord.seq",packageNo);
        }*/
//        query.like(StringUtils.isNotBlank(reportDetailDto.getWoNo()),"ord.work_order_code",reportDetailDto.getWoNo());
        query.like(StringUtils.isNotBlank(reportDetailDto.getWorkerNo()),"record.work_staff_code",reportDetailDto.getWorkerNo());
        query.like(StringUtils.isNotBlank(reportDetailDto.getPlmid()),"record.plmid",reportDetailDto.getPlmid());
        query.like(StringUtils.isNotBlank(reportDetailDto.getPhase()),"record.phase_code",reportDetailDto.getPhase());
        IPage<ReportDetail> iPage = workReportMapper.getReportDetail(reportDetailDto.toPage(), query);
        //bean转换
        IPage<ReportDetailVo> returnPage = new Page<>();
        List<ReportDetailVo> reportDetailVoList = new ArrayList<>();
        List<ReportDetail> records = iPage.getRecords();
        if (ObjectUtils.isEmpty(records)) {
            Result.success(returnPage);
        }
        for (ReportDetail record : records) {
            ReportDetailVo vo = new ReportDetailVo();
            BeanUtils.copyProperties(record, vo);
  /*          if (ObjectUtils.isNotEmpty(record.getPackageNo())) {
                vo.setPackageNo(PkgUtils.gen(record.getPackageNo()));
            }*/
            reportDetailVoList.add(vo);
        }
        BeanUtils.copyProperties(iPage, returnPage);
        returnPage.setRecords(reportDetailVoList);
        return Result.success(returnPage);
    }
    /**
     * @Description: 报工记录汇总数据
     *
     * @author: hejiahui
     * @since: 2024/10/23 8:06
     */
    public Result<IPage<ReportDetailVo>> getReportMerge(ReportDetailDto reportDetailDto) {
        //默认今天
        if (StringUtils.isBlank(reportDetailDto.getStartTime())) {
            reportDetailDto.setStartTime(DateUtil.date().toDateStr());
        }
        if (StringUtils.isBlank(reportDetailDto.getEndTime())) {
            reportDetailDto.setEndTime(DateUtil.date().offset(DateField.DAY_OF_MONTH, 1).toDateStr());
        }

        //分页获取报工详情记录
//        Page page = new Page(reportDetailDto.getPageNum(), reportDetailDto.getPageSize());
        QueryWrapper query = new QueryWrapper();
        query.between("IFNULL(record.work_time,record.create_date_time)", reportDetailDto.getStartTime() + " 00:00:00", reportDetailDto.getEndTime() + " 23:59:59");
        query.like(StringUtils.isNotBlank(reportDetailDto.getConstructNo()),"record.construct_no",reportDetailDto.getConstructNo());
     /*   if (StringUtils.isNotBlank(reportDetailDto.getPackageNo()) && reportDetailDto.getPackageNo().startsWith("WP-")) {
            Integer packageNo = PkgUtils.rollBack(reportDetailDto.getPackageNo());
            query.eq("aord.seq",packageNo);
        }*/
       /* query.like(StringUtils.isNotBlank(reportDetailDto.getWoNo()),"ord.work_order_code",reportDetailDto.getWoNo());*/
        query.like(StringUtils.isNotBlank(reportDetailDto.getWorkerNo()),"record.work_staff_code",reportDetailDto.getWorkerNo());
        query.like(StringUtils.isNotBlank(reportDetailDto.getPlmid()),"record.plmid",reportDetailDto.getPlmid());
        query.like(StringUtils.isNotBlank(reportDetailDto.getPhase()),"record.phase_code",reportDetailDto.getPhase());
        IPage<ReportDetail> iPage = workReportMapper.getReportMerge(reportDetailDto.toPage(), query);
        //bean转换
        IPage<ReportDetailVo> returnPage = new Page<>();
        List<ReportDetailVo> reportDetailVoList = new ArrayList<>();
        List<ReportDetail> records = iPage.getRecords();
        if (ObjectUtils.isEmpty(records)) {
            Result.success(returnPage);
        }
        for (ReportDetail record : records) {
            ReportDetailVo vo = new ReportDetailVo();
            BeanUtils.copyProperties(record, vo);
//            vo.setPackageNo(PkgUtils.gen(record.getPackageNo()));
            reportDetailVoList.add(vo);
        }
        BeanUtils.copyProperties(iPage, returnPage);
        returnPage.setRecords(reportDetailVoList);
        return Result.success(returnPage);
    }
}
